/**
 *
 * @description: 针对微信后端控制器
 * @author: junyong.hong
 * @createTime: 2018/11/20
 * @version: 1.0.0.0
 * @history:
 *    1、
 *    2、
 *
 */
// 回复策略
const { reply } = require('../../wechat/reply')
const config = require('../../config/config')
const api = require('../api/index')
// 消息中间件
const wechatMiddle = require('../../wechat-lib/middleware')

exports.getSDKSignature = async (ctx, next) => {
    let url = ctx.query.url
    url = decodeURIComponent(url)
    const params = await api.wechat.getSignature(url)

    ctx.body = {
        success: true,
        data: params
    }
}

exports.sdk = async (ctx, next) => {
    const url = ctx.href

    // 调用的是app/api/wechat.js里的方法
    const params = await api.wechat.getSignature(url)


    await ctx.render('wechat/sdk', params)
}

// 接入微信消息中间件
exports.hear = async (ctx, next) => {
    const middle = wechatMiddle(config.wechat, reply)

    await middle(ctx, next)
}

// 网页授权
exports.oauth = async (ctx, next) => {
    const state = ctx.query.id
    const scope = 'snsapi_userinfo'
    const target = config.baseUrl + 'userinfo'
    const url = api.wechat.getAuthorizeURL(scope, target, state)
    ctx.redirect(url)
}

exports.userinfo = async (ctx, next) => {
    // const oauth = getOAuth()
    // const code = ctx.query.code
    // const data = await oauth.fetchAccessToken(code)
    // const userData = await oauth.getUserInfo(data.access_token, data.openid)

    const userData = await api.wechat.getUserinfoByCode(ctx.query.code)

    ctx.body = userData
}

function isWechat(ua) {
    if (ua.indexOf('MicroMessenger') >= 0) {
        return true
    } else {
        return false
    }
}

exports.checkWechat = async (ctx, next) => {
    const ua = ctx.headers['user-agent']
    const code = ctx.query.code

    // 所有的网页请求都会流经这个中间件，包括微信的网页访问
    // 针对POST 非 GET 请求，不走用户授权流程
    if (ctx.method === 'GET') {
        // 如果参数带code，说明用户已经授权
        if (code) {
            await next()
        } else if (isWechat(ua)) {  // 如果没有code，且来自微信访问，就可以配置授权的跳转
            const target = ctx.href
            const scope = 'snsapi_userinfo'
            const url = await api.wechat.getAuthorizeURL(scope, target, 'fromWechat')

            ctx.redirect(url)
        } else {
            await next()
        }
    } else {
        await next()
    }
}


exports.wechatRedirect = async (ctx, next) => {
    const { code, state } = ctx.query

    if (code && state === 'fromWechat') {
        const userData = await api.wechat.getUserinfoByCode(code)
        const user = await api.wechat.saveWechatUser(userData)

        ctx.session.user = {
            _id: user._id,
            role: user.role,
            nickname: user.nickname
        }

        ctx.state = Object.assign(ctx.state, {
            user: {
                _id: user._id,
                role: user.role,
                nickname: user.nickname
            }
        })
    }

    await next()
}